// 参考https://www.cnblogs.com/jackson-yqj/p/10154296.html

'use strict';
const nodemailer = require('nodemailer');

function mailSender(){
    // this.smtp = 'smtp.163.email';
    this.mailFrom = 'wzh_0206@163.com';
    this.mailPwd = 'WIQTDOREHZVQGTVJ';
    this.send = function(email, code, callback) {
        var transporter = nodemailer.createTransport({
            // host: smtp,
            service: '163', // 使用了内置传输发送邮件 查看支持列表：https://nodemailer.com/smtp/well-known/
            port: 465, // SMTP 端口
            secureConnection: true, // 使用了 SSL
            auth: {
                user: this.mailFrom,
                // 这里密码不是163密码，是你设置的smtp授权码，去163邮箱后台开通、查看https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html
                pass: this.mailPwd,
            }
        });
    
        var mailOptions = {
            from: this.mailFrom, // sender address
            to: email, // list of receivers
            subject: 'wzhtest', // Subject line
            text: `【wzhtest】验证码:${code}`,
        };
    
        var result = {
            httpCode: 200,
            message: '发送成功！',
        }
    
        try {
            transporter.sendMail(mailOptions, function (err, info) {
                if (err) {
                    result.httpCode = 500;
                    result.message = err;
                    callback(result);
                    return;
                }
                callback(result);
            });
        } catch (err) {
            result.httpCode = 500;
            result.message = err;
            callback(result);
        }
    }
}

var mailSender = new mailSender();

module.exports = mailSender